﻿//-- =============================================
//-- Author:	    Hossein-Nejad, Saghari
//-- Create date:   1389/12/11
//-- Last Update:   1390/01/23 22:22    By: Amir
//-- Description:	Data Access Layer Base Class
//-- =============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;



namespace ChequeManagement.DAL
{
    /// <summary>
    /// this Class contain methods for do some Transaction on dataBase
    /// </summary>
    public class BaseDAL : IDisposable
    {
         
        private static SqlConnection connection = null;
        private string connectionString = string.Empty;
        private static SqlTransaction transaction = null;
        private static SqlCommand command = null;

        // Track whether Dispose has been called.
        private bool disposed = false;

        /// <summary>
        /// this constructor get a connectionString from app.config
        /// </summary>
        public BaseDAL()
        {
            this.connectionString = Properties.Settings.Default.Database1ConnectionString;
        }

        /// <summary>
        /// this constructor get a parameter that other class that this parameter value is connectionString
        /// </summary>
        /// <param name="connectionString" Type="string"></param>
        public BaseDAL(string connectionString)
        {
            this.connectionString = connectionString;
        }
   
        /// <summary>
        /// this method check connection,if not connection, create connection else return connection
        /// </summary>
        public  SqlConnection Connection
        {
            get
            {
                if (connection == null)
                    connection = new SqlConnection(this.connectionString);
                return connection;
            }
        }

        /// <summary>
        /// this method return your connection state
        /// </summary>
        public ConnectionState ConnectionState
        {
            get
            {
                return this.Connection.State;
            }
        }

        /// <summary>
        /// this method check your command,if not command,create a command and command connection is this connection
        /// </summary>
        protected SqlCommand Command
        {
            get
            {
                if (command == null)
                {
                    command = new SqlCommand();
                    command.Connection = this.Connection;
                }
                return command;
            }
        }

        /// <summary>
        /// this method check your connection,if connection is close,open connection and begin Transaction
        /// </summary>
        public void BeginTransaction()
        {
            try
            {
                if (this.ConnectionState == System.Data.ConnectionState.Closed)
                {
                    this.Connection.Open();
                    transaction = this.Connection.BeginTransaction();
                    this.Command.Transaction = transaction;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// this method check connection,if connection is open and Transaction is begin,Commit transaction
        /// </summary>
        public void CommitTransaction()
        {
            try
            {
                if (transaction != null && this.ConnectionState == System.Data.ConnectionState.Open)
                {
                    transaction.Commit();
                    this.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.Dispose(true);
            }
        }

        /// <summary>
        /// this method check connection,if connection is open and Transaction is begin,Undo transaction
        /// </summary>
        public void RollBackTransaction()
        {
            try
            {
                if (transaction != null && this.ConnectionState == System.Data.ConnectionState.Open)
                {
                    transaction.Rollback();
                    this.Connection.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.Dispose(true);
            }
        }

        /// <summary>
        /// this mathod Dispose connection  then call garbage collector,garbage collector call SuppressFinalize method for take off this object from queue
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);

            // This object will be cleaned up by the Dispose method.
            // Therefore, you should call GC.SupressFinalize to
            // take this object off the finalization queue
            // and prevent finalization code for this object
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// this mathod Dispose you command,connection
        /// if parameter equals true, dispose all managed code and unmanaged code, 
        ///else only dispose unmanaged code
        /// </summary>
        /// <param name="bIsDisposing"></param>
        protected virtual void Dispose(bool bIsDisposing)
        {
            // Check to see if Dispose has already been called.
            if (!this.disposed)
            {
                //if bIsDisposing equals true, dispose all managed code and unmanaged code, 
                //else only dispose unmanaged code
                if (bIsDisposing)
                {
                    if (command != null)
                    {
                        command.Dispose();
                        command = null;
                    }
                    if (connection != null)
                    {
                        connection.Dispose();
                        connection = null;
                    }
                    if (transaction != null)
                    {
                        transaction.Dispose();
                        transaction = null;
                    }
                }

                if (this.ConnectionState != System.Data.ConnectionState.Closed)
                    this.Connection.Close();
                
                //disposing has been done
                this.disposed = true;
                
            }
        }
        /// <summary>
        /// this destructor call Dispose method and set parameter
        /// parameter value is false
        /// </summary>
        ~BaseDAL()
        {
            Dispose(false);
        }
    }
}
